/****************************************************************************
 *                                                                          *
 * File    : Lottery.c                                                      *
 *                                                                          *
 * Purpose : Console mode (command line) program.                           *
 *                                                                          *
 * History : Date                       Reason                              *
 *           Long time ago              Created                             *
 *                                                                          *
 *           2014/09/24                 restart the pro                     *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>

/****************************************************************************
 *                                                                          *
 *　“双色球”每注投注号码由6个红色球号码
 *  和1个蓝色球号码组成。
 *  红色球号码从1—33中选择；
 *  蓝色球号码从1—16中选择。
 */


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM_RED_MAX 33
#define NUM_BLUE_MAX 16

int red_balls[NUM_RED_MAX], blue_balls[NUM_BLUE_MAX];

void init_balls(void)
{
    int ball = 0;

    for(ball = 0; ball < NUM_RED_MAX; ball++)
        red_balls[ball] = ball + 1;

    for(ball = 0; ball < NUM_BLUE_MAX; ball++)
        blue_balls[ball] = ball + 1;
}

void init_srand(int s)
{
    if (s < 0)
        srand((unsigned)time(NULL));
    else
        srand(s);
}

void dump_balls(void)
{
    int ball = 0;
    printf("\n");
    for(ball = 0; ball < NUM_RED_MAX; ball++)
        printf("%d ", red_balls[ball]);
    printf("\n");
    for(ball = 0; ball < NUM_BLUE_MAX; ball++)
        printf("%d ", blue_balls[ball]);
    printf("\n");
}

void BubbleSort(int * pData,int Count)
{
    int iTemp = 0, i = 0, j = 0;
    for (i=0; i<Count; i++)
    {
        for (j=Count-1; j>i; j--)
        {
            if (pData[j]<pData[j-1])
            {
                iTemp = pData[j-1];
                pData[j-1] = pData[j];
                pData[j] = iTemp;
            }
        }
    }
}
int get_red_num(void) {
    int index = 0;
    int red_ball = 0;

    while (!red_ball) {
        index = rand()%(NUM_RED_MAX );
        red_ball = red_balls[index];
    }

    red_balls[index] = 0;
    return red_ball;
}

int get_blue_num(void) {
    int index = 0;
    int blue_ball = 0;

    while (!blue_ball) {
        index = rand()%(NUM_BLUE_MAX );
        blue_ball = blue_balls[index];
    }

    blue_balls[index] = 0;
    return blue_ball;
}
int check_dup(int *arr, int n) {
    int dup = 0, i = 0, j = 0;
    for (i=0; i<n; ++i)
        for(j=0; j<n; ++j)
            if(arr[i] == arr[j] && i != j)
                dup = 1;
    return dup;
}

int choose_record(int *record) {
    int red_turn = 6, blue_turn = 1;
    int i = 0;

    init_balls();
CHOOSE:
    for (i = 0; i < red_turn; i++)
        *(record+i) = get_red_num();
    if(check_dup(record, 6))
        goto CHOOSE;
    BubbleSort(record, 6);

    for (i = 0; i < blue_turn; i++)
        *(record+red_turn+i) = get_blue_num();
}

int Winning(int *rand, int *dest)
{
    int flag = 0, sum_num = 0;
    int tatol[7] = {0};
    for (; flag < 7; flag++) {
        if (  *(rand+flag) == *(dest + flag) )
            tatol[flag]++ ;
    }
    for (flag = 0; flag < 6; flag++)
        sum_num += tatol[flag];

    switch (sum_num) {
    case 6:
        return tatol[6] ? 1 : 2;
    case 5:
        return tatol[6] ? 3 : 4;
    case 4:
        return tatol[6] ? 4 : 5;
    case 3:
        return tatol[6] ? 5 : 0;
    case 2:
    case 1:
        return tatol[6] ? 6 : 0;
    default:
        return 0;
    }

}

int put_num(int *arr)
{
    int i = 0;
    for (i = 0; i < 6 ; i++)
        printf("%2d ",arr[i]);
    printf("| %2d",arr[6]);
}

void print_menu(void)
{
    printf("\n\n\tWinning the big prize\n");
    printf("\t\tMenu\n");
    printf("\t1)random numbers\n");
    printf("\t2)Input numbers and get the prize\n");


}
int main(int argc, char **argv)
{
    int rec[7], i, sch[7] = {0},count = 0, w = 0, t = 0;
    int win[6] = {0};
MENU:
    print_menu();

    init_srand(-1);
    printf("\n\tYou choise(1 or 2)?");
    char c = getchar();
    if( c == '1')
    {
        choose_record(rec);
        put_num(rec);
        printf("\n");
    }
    else if (c == '2') {
        printf("Input 7 numbers with space to part:");
        for (i = 0; i < 7 ; i++)
            scanf("%d",&rec[i]);
        while (1)
        {
            t = win[0];
            w = Winning(rec, sch);
            win[w-1]++;
            choose_record(sch);
            count++;
            //put_num(sch);
            //printf("  -> count = %d, \n",count);
            if (t != win[0])
                printf(" %2d %4d %6d %7d %8d %10d Count: %12d\n",win[0], win[1], win[2], win[3], win[4], win[5], count);
        }
    }
    else
        printf("Not functional number!");
    getchar();
    goto MENU;
    return 0;
}
